<?php
/**
 * Created by PhpStorm.
 * User: ytl003
 * Date: 2018/10/2
 * Time: 下午11:23
 */

namespace app\api\controller;


use app\api\model\UserModel;
use app\api\service\Back;
use app\api\service\TokenService;
use app\lib\exception\ErrorException;
use think\facade\Request;

class User
{
    /**
     * @route('user/info')
     */
    public function info(){
        $userId = TokenService::getCurrentUid();
        $user = UserModel::get($userId);
        $user['access'] = '';
//        $user = getMd5Password('YyByYTL2018');
        return Back::success($user);
    }

    /**
     * @return array
     * @throws ErrorException
     * @throws \think\Exception\DbException
     * @route('user/password')
     */
    public function changePassword(){
        $data = Request::post();
        $userId = TokenService::getCurrentUid();
        $user = UserModel::get($userId);
        $password = $user['password'];
        $data['oldPassword'] = getMd5Password($data['oldPassword']);
        $data['newPassword'] = getMd5Password($data['newPassword']);
        $data['rePassword'] = getMd5Password($data['rePassword']);
        if (!$data['newPassword'])
            throw new ErrorException(['msg' => '请输入需要修改的密码']);
        if ($data['newPassword'] != $data['rePassword'])
            throw new ErrorException(['msg' => '两次输入密码不正确']);
        if ($user['password'] != $data['oldPassword'])
            throw new ErrorException(['msg' => '原密码错误']);
        $user->password = $data['newPassword'];
        $user->save();
        return Back::success([
            $user['password'],$password
        ]);
    }
}